package assist.domain.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


/**
 * A Map of RosterPlayers, with the sweater number as the key to the Map.
 */
@PersistenceCapable(detachable = "true")
public class Roster implements Serializable
{
    @NotPersistent
    private static final long serialVersionUID = 1L;    
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long key;
    @Persistent(defaultFetchGroup="true")   
    private  List<RosterPlayer> rosterPlayers =  new ArrayList<RosterPlayer>();
    
    //TODO: Need Team ID and Game ID for mapping and queries.

    
    public boolean add(RosterPlayer rosterPlayer)
    {
        return this.rosterPlayers.add(rosterPlayer);
    }
    
    public RosterPlayer get(Integer num)
    {
        for (RosterPlayer rosterPlayer : this.rosterPlayers)
        {
            if (num.equals(rosterPlayer.getNumber()))
            {
                return rosterPlayer;
            }
        }
        return null;
    }
    
    /**
     * Convenience method that returns the Player with the given sweaterNum
     * from the RosterPlayer onthis Roster.
     * @param sweaterNum
     * @return The Player that has the given sweaterNumber on this Roster.
     */
    public Player getPlayer(final Integer sweaterNum)
    {        
        RosterPlayer rosterPlayer = this.get(sweaterNum);
        return rosterPlayer == null ? null: rosterPlayer.getPlayer();
    }
    /**
     * @param key the key to set
     */
    public void setKey(Long key)
    {
        this.key = key;
    }
    /**
     * @return the key
     */
    public Long getKey()
    {
        return key;
    }
}
